const { ethers } = require("ethers");
const fs = require("fs-extra");
require("dotenv").config();

async function main() {
  const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
  // 简单连接的方法
  const wallet = new ethers.Wallet(process.env.PRIVATE_KEY || "", provider);
  // 使用加密连接
  // const encryptedJson = fs.readFileSync("./.encryptedKey.json", "utf8");
  // let wallet = ethers.Wallet.fromEncryptedJsonSync(
  //   encryptedJson,
  //   process.env.PRIVATE_KEY_PASSWORD
  // );
  // wallet = wallet.connect(provider);
  const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf8");
  const binary = fs.readFileSync(
    "./SimpleStorage_sol_SimpleStorage.bin",
    "utf8"
  );

  const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log("deploying, please wait ...");

  const contract = await contractFactory.deploy();
  const deploymentReceipt = await contract.waitForDeployment(1);
  console.log(`Contract deployed to ${await contract.getAddress()}`);

  let myAge = await contract.getAge();
  console.log(`my age is ${myAge}`);

  let transactionResponse = await contract.store(30);
  console.log("is changing age ...");

  let transactionReceipt = await transactionResponse.wait(1);
  myAge = await contract.getAge();
  console.log(`now my age is changed: ${myAge}`);

  // // 指定一个交易
  // const nonce = await wallet.getNonce();
  // const tx = {
  //   nonce,
  //   // 预计消耗的gas
  //   gasPrice: 100000000000,
  //   // 最大消耗
  //   gasLimit: 1000000,
  //   to: null,
  //   value: 0,
  //   data: "0x608060405234801561001057600080fd5b5061083a806100206000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c80633bc862c71461005c57806390c3ee4d14610078578063967e6e65146100a85780639e7a13ad146100c6578063eb49a6f0146100f7575b600080fd5b61007660048036038101906100719190610512565b610113565b005b610092600480360381019061008d9190610440565b610130565b60405161009f91906105ff565b60405180910390f35b6100b0610166565b6040516100bd91906105ff565b60405180910390f35b6100e060048036038101906100db91906104e5565b61017c565b6040516100ee9291906105cf565b60405180910390f35b610111600480360381019061010c9190610489565b610245565b005b806000806101000a81548160ff021916908360ff16021790555050565b6002818051602081018201805184825260208301602085012081835280955050505050506000915054906101000a900460ff1681565b60008060009054906101000a900460ff16905090565b6001818154811061018c57600080fd5b90600052602060002090600202016000915090508060000180546101af906106f0565b80601f01602080910402602001604051908101604052809291908181526020018280546101db906106f0565b80156102285780601f106101fd57610100808354040283529160200191610228565b820191906000526020600020905b81548152906001019060200180831161020b57829003601f168201915b5050505050908060010160009054906101000a900460ff16905082565b600160405180604001604052808481526020018360ff16815250908060018154018082558091505060019003906000526020600020906002020160009091909190915060008201518160000190805190602001906102a4929190610303565b5060208201518160010160006101000a81548160ff021916908360ff1602179055505050806002836040516102d991906105b8565b908152602001604051809103902060006101000a81548160ff021916908360ff1602179055505050565b82805461030f906106f0565b90600052602060002090601f0160209004810192826103315760008555610378565b82601f1061034a57805160ff1916838001178555610378565b82800160010185558215610378579182015b8281111561037757825182559160200191906001019061035c565b5b5090506103859190610389565b5090565b5b808211156103a257600081600090555060010161038a565b5090565b60006103b96103b48461063f565b61061a565b9050828152602081018484840111156103d5576103d46107b6565b5b6103e08482856106ae565b509392505050565b600082601f8301126103fd576103fc6107b1565b5b813561040d8482602086016103a6565b91505092915050565b600081359050610425816107d6565b92915050565b60008135905061043a816107ed565b92915050565b600060208284031215610456576104556107c0565b5b600082013567ffffffffffffffff811115610474576104736107bb565b5b610480848285016103e8565b91505092915050565b600080604083850312156104a05761049f6107c0565b5b600083013567ffffffffffffffff8111156104be576104bd6107bb565b5b6104ca858286016103e8565b92505060206104db8582860161042b565b9150509250929050565b6000602082840312156104fb576104fa6107c0565b5b600061050984828501610416565b91505092915050565b600060208284031215610528576105276107c0565b5b60006105368482850161042b565b91505092915050565b600061054a82610670565b610554818561067b565b93506105648185602086016106bd565b61056d816107c5565b840191505092915050565b600061058382610670565b61058d818561068c565b935061059d8185602086016106bd565b80840191505092915050565b6105b2816106a1565b82525050565b60006105c48284610578565b915081905092915050565b600060408201905081810360008301526105e9818561053f565b90506105f860208301846105a9565b9392505050565b600060208201905061061460008301846105a9565b92915050565b6000610624610635565b90506106308282610722565b919050565b6000604051905090565b600067ffffffffffffffff82111561065a57610659610782565b5b610663826107c5565b9050602081019050919050565b600081519050919050565b600082825260208201905092915050565b600081905092915050565b6000819050919050565b600060ff82169050919050565b82818337600083830152505050565b60005b838110156106db5780820151818401526020810190506106c0565b838111156106ea576000848401525b50505050565b6000600282049050600182168061070857607f821691505b6020821081141561071c5761071b610753565b5b50919050565b61072b826107c5565b810181811067ffffffffffffffff8211171561074a57610749610782565b5b80604052505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b6107df81610697565b81146107ea57600080fd5b50565b6107f6816106a1565b811461080157600080fd5b5056fea26469706673582212201facb3175ba28e052ccb1522c2f76f329e3492abc031dc15427c0950e03a5f5f64736f6c63430008070033",
  //   chainId: 1337, // 固定是1337 这是 ganache 固定的端口
  // };

  // console.log("Let's deploy another! Please wait...");
  // let resp = await wallet.signTransaction(tx);
  // const sentTxResponse = await wallet.sendTransaction(tx);
  // console.log(resp);
  // console.log(sentTxResponse, "sentTxResponse");
}

main();
